package com.zlq.login.controller;



import com.zlq.login.dao.UserDaoLo;
import com.zlq.login.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/insertController")
public class InsertController extends HttpServlet {
    private UserDaoLo ud = new UserDaoLo();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String name = req.getParameter("name");
        String password = req.getParameter("password");

        if ((!name.isEmpty() || !name.equals(" ")) && (!password.isEmpty() || !password.equals(" "))) {
            ud.save(new User(name, password));
//            resp.sendRedirect("list.do");
            req.getRequestDispatcher("/listController").forward(req,resp);
        } else {
            resp.sendRedirect("saveUser.jsp");
        }

    }
}
